package com.jeecg.p3.system.advisor.web;

import com.jeecg.p3.base.util.DateUtil;
import com.jeecg.p3.system.advisor.entity.StuAdvGroup;
import com.jeecg.p3.system.advisor.service.StuAdvGroupService;
import com.jeecg.p3.system.base.BaseController;
import com.jeecg.p3.system.entity.JwSystemRole;
import com.jeecg.p3.system.entity.JwSystemUser;
import com.jeecg.p3.system.util.Constants;
import com.jeecg.p3.system.vo.LoginUser;
import com.jeecg.p3.system.vo.MenuMutex;
import org.apache.velocity.VelocityContext;
import org.jeecgframework.p3.core.common.utils.AjaxJson;
import org.jeecgframework.p3.core.common.utils.StringUtil;
import org.jeecgframework.p3.core.util.SystemTools;
import org.jeecgframework.p3.core.util.plugin.ViewVelocity;
import org.jeecgframework.p3.core.utils.common.PageQuery;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;

/**
 * 学习顾问分组控制层
 * Created on 2020-08-28$ 21:30$
 *
 * @author 张晓$
 * @version V1.0
 */
@Controller
@RequestMapping("/system/back/advisor")
public class StuAdvGroupController extends BaseController {
    public final Logger log = LoggerFactory.getLogger(getClass());

    @Autowired
    private StuAdvGroupService advGroupService;

    /**
     *
     * @param query 群组查询对象
     * @param request
     * @param response
     * @param pageNo 第几页
     * @param pageSize 页数大小
     * @throws Exception
     */
    @RequestMapping(value="/group/list.do",method = {RequestMethod.GET,RequestMethod.POST})
    public void toIndividual(@ModelAttribute StuAdvGroup query, HttpServletRequest request, HttpServletResponse response,
                             @RequestParam(required = false, value = "pageNo", defaultValue = "1") int pageNo,
                             @RequestParam(required = false, value = "pageSize", defaultValue = "10") int pageSize) throws Exception{
        PageQuery<StuAdvGroup> pageQuery = new PageQuery<StuAdvGroup>();
        pageQuery.setPageNo(pageNo);
        pageQuery.setPageSize(pageSize);
        VelocityContext velocityContext = new VelocityContext();
        pageQuery.setQuery(query);
        velocityContext.put("group",query);
        velocityContext.put("pageInfos", SystemTools.convertPaginatedList(advGroupService.queryPageList(pageQuery)));
        String viewName = "system/back/advisor/stuAdvGroup.vm";
        ViewVelocity.view(request,response,viewName,velocityContext);
    }

    /**
     * 跳转到新增群组界面页面
     * @return
     */
    @RequestMapping(value = "/group/toAdd",method ={RequestMethod.GET, RequestMethod.POST})
    public void toAddDialog(HttpServletRequest request, HttpServletResponse response, ModelMap model)throws Exception{
        VelocityContext velocityContext = new VelocityContext();
        String viewName = "system/back/advisor/stuAdvGroup_add.vm";
        ViewVelocity.view(request,response,viewName,velocityContext);
    }

    /**
     * 保存信息
     * @return
     */
    @RequestMapping(value = "/group/doAdd",method ={RequestMethod.GET, RequestMethod.POST})
    @ResponseBody
    public AjaxJson doAdd(@ModelAttribute StuAdvGroup stuAdvGroup, HttpServletRequest request){
        AjaxJson j = new AjaxJson();
        try {
            LoginUser user = getCurrentUser(request);
            stuAdvGroup.setCreateDate(DateUtil.dateToString(new Date()));
            stuAdvGroup.setCreateTime(DateUtil.dateToString(new Date(), "HH:mm:ss"));
            stuAdvGroup.setCreateUser(user.getId());
            advGroupService.doAdd(stuAdvGroup);
            j.setMsg("保存成功");
        } catch (Exception e) {
            log.info(e.getMessage());
            j.setSuccess(false);
            j.setMsg("保存失败");
        }
        return j;
    }

    /**
     * 跳转到编辑页面
     * @return
     */
    @RequestMapping(value="/group/toEdit",method = RequestMethod.GET)
    public void toEdit(@RequestParam(required = true, value = "id" ) Long id,HttpServletResponse response,HttpServletRequest request) throws Exception{
        VelocityContext velocityContext = new VelocityContext();
        StuAdvGroup stuAdvGroup = advGroupService.findById(id);
        velocityContext.put("stuAdvGroup",stuAdvGroup);
        String viewName = "system/back/advisor/stuAdvGroup_edit.vm";
        ViewVelocity.view(request,response,viewName,velocityContext);
    }

    /**
     * 保存修改数据
     * @return
     */
    @RequestMapping(value = "/group/doEdit",method ={RequestMethod.GET, RequestMethod.POST})
    @ResponseBody
    public AjaxJson doEdit(@ModelAttribute StuAdvGroup stuAdvGroup, HttpServletRequest request){
        AjaxJson j = new AjaxJson();
        try {
            LoginUser user = getCurrentUser(request);
            stuAdvGroup.setUpdateDate(DateUtil.dateToString(new Date()));
            stuAdvGroup.setUpdateTime(DateUtil.dateToString(new Date(), "HH:mm:ss"));
            stuAdvGroup.setUpdateUser(user.getId());
            this.advGroupService.doEdit(stuAdvGroup);
            j.setMsg("编辑成功");
        } catch (Exception e) {
            e.printStackTrace();
            log.info(e.getMessage());
            j.setSuccess(false);
            j.setMsg("编辑失败");
        }
        return j;
    }

    /**
     * 删除
     * @return
     */
    @RequestMapping(value="/group/doDelete",method = RequestMethod.GET)
    @ResponseBody
    public AjaxJson doDelete(@RequestParam(required = true, value = "id" ) Long id, HttpServletRequest request){
        AjaxJson j = new AjaxJson();
        try {
            LoginUser user = getCurrentUser(request);
            StuAdvGroup stuAdvGroup = advGroupService.findById(id);
            stuAdvGroup.setDeleteDate(DateUtil.dateToString(new Date()));
            stuAdvGroup.setDeleteTime(DateUtil.dateToString(new Date(), "HH:mm:ss"));
            stuAdvGroup.setDeleteUser(user.getId());
            advGroupService.doDelete(stuAdvGroup);
            j.setMsg("删除成功");
        } catch (Exception e) {
            log.info(e.getMessage());
            j.setSuccess(false);
            j.setMsg("删除失败:"+e.getMessage());
        }
        return j;
    }
}
